#include "addfrienddialog.h"
#include <QPushButton>
#include <QScrollArea>
#include <QScrollBar>
#include <QLabel>
#include "debug.h"
#include "model/datacenter.h"

AddFriendItem::AddFriendItem(const model::UserInfo& userInfo)
{
    //1.设置布局管理器
    QGridLayout* layout = new QGridLayout(this);

    //2.添加组件
    this->setFixedHeight(70);
    QPushButton* avatarBtn = new QPushButton;
    avatarBtn->setIcon(userInfo.avatar);
    avatarBtn->setFixedSize(45,45);
    avatarBtn->setIconSize(QSize(45,45));
    avatarBtn->setStyleSheet("QPushButton{border:none;}");

    QLabel* nameLabel = new QLabel;
    nameLabel->setText(userInfo.nickname);
    QFont font("微软雅黑",14,400);
    nameLabel->setFont(font);

    QLabel* descLabel = new QLabel;
    descLabel->setText(userInfo.description);

    QPushButton* addBtn = new QPushButton;
    addBtn->setFixedSize(100,40);
    QString style = "QPushButton{border:1px solid grey;background-color:rgb(137, 217, 97);border-radius:5px;}";
    style+="QPushButton:hover{background-color:rgb(127, 207, 87);}";
    style+="QPushButton:pressed{background-color:rgb(107, 187, 67);}";
    addBtn->setStyleSheet(style);
    addBtn->setText("添加好友");

    layout->addWidget(avatarBtn,0,0,2,1);
    layout->addWidget(nameLabel,0,1,1,6);
    layout->addWidget(descLabel,1,1,1,6);
    layout->addWidget(addBtn,0,7,2,2);

    //添加信号和槽
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(addBtn,&QPushButton::clicked,this,[=]()
    {
        dataCenter->sendApplyAsync(userInfo.userId);
        addBtn->setEnabled(false);
    });

}


AddFriendDialog::AddFriendDialog(QWidget* parent)
{
    //1.设置属性
    this->setWindowTitle("添加好友");
    this->setFixedSize(550,550);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget{background-color:white;}");

    //2.设置布局管理器
    QGridLayout* layout = new QGridLayout(this);

    //3.添加控件
    searchEdit = new QLineEdit;
    searchEdit->setFixedHeight(40);

    QPushButton* searchBtn = new QPushButton;
    searchBtn->setFixedSize(35,35);
    searchBtn->setIcon(QIcon(":/myResource/Image/search.svg"));
    searchBtn->setIconSize(QSize(25,25));

    QScrollArea* scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0;}");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:3px;}");


    QWidget*  container = new QWidget;
    scrollArea->setWidget(container);

    vLayout = new QVBoxLayout(container);
    vLayout->setAlignment(Qt::AlignTop);

    layout->addWidget(searchEdit,0,0,1,8);
    layout->addWidget(searchBtn,0,8,1,1);
    layout->addWidget(scrollArea,1,0);

    //测试代码
#if TEST_UI
    for(int i =0;i<20;i++)
    {
        model::UserInfo u;
        u.userId=QString::number(i);
        u.nickname = "张三"+QString::number(i);
        u.description = "one 签名";
        u.avatar = QIcon(":/myResource/Image/dogAvatar.svg");
        addItem(u);
    }
    clear();
    for(int i =0;i<20;i++)
    {
        model::UserInfo u;
        u.userId=QString::number(i);
        u.nickname = "张三"+QString::number(i);
        u.description = "one 签名";
        u.avatar = QIcon(":/myResource/Image/dogAvatar.svg");
        addItem(u);
    }

#endif

    connect(searchBtn,&QPushButton::clicked,this,[=]()
    {
        const QString& key = searchEdit->text();
        if(key.isEmpty()) return;
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter,&model::DataCenter::searchUserDone,this,&AddFriendDialog::clickSearchBtnDone);
        dataCenter->sreachUserAsync(key);
    });
}
void AddFriendDialog::addItem(const model::UserInfo& userInfo)
{
    AddFriendItem* item = new AddFriendItem(userInfo);
    this->vLayout->addWidget(item);
}
void AddFriendDialog::clear()
{
    for(int i =vLayout->count()-1;i>=0;i--)
    {
        QLayoutItem* item = vLayout->takeAt(i);
        if(item&&item->widget())
            delete item->widget();
    }
}
void AddFriendDialog::setSearchKey(const QString& key)
{
    searchEdit->setText(key);
}

void AddFriendDialog::clickSearchBtnDone()
{
    clear();
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(!dataCenter->getSearchUserList())
    {
        LOG()<<"搜索List为nullptr";
        return;
    }
    for(auto u:*dataCenter->getSearchUserList())
        addItem(u);
}
